/* eslint-disable import/prefer-default-export */
const tablecrud = (title, Component) => {
  return `import * as React from 'react';
  import { useState, useMemo, useRef } from 'react';
  import DrawerPro from '@/components/DrawerPro';
  import AutoTable from '@/components/AutoTable';
  import PremButton from '@/components/PremButton';
  import getcolumns from './columns';
  import { useRequest } from 'ahooks';
  import { doFetch } from '@/utils/doFetch';

  function ${Component}(props) {
    const actionRef = useRef(),
      formRef = useRef();
    const [drawer, setdrawer] = useState({
      open: false,
    });
    const pathconfig = useMemo(() => {
      let pathconf = getcolumns(setdrawer)?.pathconfig ?? {};
      return pathconf;
    }, []);
    const { run, loading } = useRequest(doFetch, {
      manual: true,
      onSuccess: (res, params) => {
        if (res?.code == '0000') {
          actionRef?.current?.reload();
          setdrawer((s) => ({
            ...s,
            open: false,
          }));
        }
      },
    });

    const detail = (text, row, _, action) => {
      return (
        <PremButton
          btn={{
            size: 'small',
            type: 'link',
            onClick: () => {
              setdrawer((s) => ({
                ...s,
                open: true,
                item: row,
                title: '详情',
                val: 'detail',
                title: '详细信息',
              }));
            },
          }}
        >
          详情
        </PremButton>
      );
    };

    const edit = (text, row, _, action) => {
      return (
        <PremButton
          btn={{
            size: 'small',
            onClick: () => {
              setdrawer((s) => ({
                ...s,
                open: true,
                item: row,
                title: '编辑',
                val: 'edit',
              }));
            },
          }}
        >
          编辑
        </PremButton>
      );
    };

    const remove = (text, row, _, action) => {
      return (
        <PremButton
          pop={{
            title: '是否删除?',
            okText: '确认',
            cancelText: '取消',
            onConfirm: () => {
              run({ url: pathconfig?.delete || '/delete', params: { id: row?.id } });
            },
          }}
          btn={{
            size: 'small',
            type: 'danger',
          }}
        >
          删除
        </PremButton>
      );
    };

    const columns = useMemo(() => {
      let defcolumn = getcolumns(setdrawer)?.columns;
      return defcolumn.concat({
        title: '操作',
        valueType: 'option',
        width: 150,
        render: (text, row, _, action) => [
          pathconfig?.enabledetail && detail(text, row, _, action),
          pathconfig?.enableedit && edit(text, row, _, action),
          pathconfig?.enabledelete && remove(text, row, _, action),
        ],
      });
    }, []);

    return (
      <div style={{ position: 'relative' }}>
        <AutoTable
          pagetitle="${title}"
          columns={columns}
          actionRef={actionRef}
          path={pathconfig?.list || '/ngic-auth/sysUser/query/page'}
          pageextra={pathconfig?.enableadd ? 'add' : null}
          resizeable={true}
          addconfig={{
            // access: 'sysDepartment_save',
            btn: {
              disabled: false,
              onClick: () => {
                setdrawer((s) => ({
                  ...s,
                  open: true,
                  item: null,
                  title: '新增',
                  val: 'add',
                }));
              },
            },
          }}
        />

        <DrawerPro
          fields={columns}
          params={{ id: drawer?.item?.id }}
          formRef={formRef}
          placement="right"
          detailpath={pathconfig?.detail || null}
          detailData={drawer?.item}
          defaultFormValue={drawer?.item}
          onClose={() => {
            setdrawer((s) => ({
              ...s,
              open: false,
            }));
          }}
          {...drawer}
          onFinish={(vals) => {
            if (drawer?.val == 'add') {
              run({ url: pathconfig?.add || '/add', params: { ...vals } });
            } else if (drawer?.val == 'edit') {
              run({ url: pathconfig?.edit || '/edit', params: { ...vals, id: drawer?.item?.id } });
            }
          }}
        />
      </div>
    );
  }

  export default ${Component};
  `;
};

const datas = {
  pathconfig: {
    enableadd: false,
    enableedit: false,
    enabledelete: false,
    enabledetail: false,
    add: '',
    edit: '',
    list: '/repair/umRepairOrder/queryRepairOrderList',
    delete: '',
    detail: '',
  },
  columns: [
    {
      title: '维修单号',
      dataIndex: 'repairOrderNo',
      key: 'repairOrderNo',
    },
    {
      title: '设备编号',
      dataIndex: 'equipmentNo',
      key: 'equipmentNo',
    },
    {
      title: '设备名称',
      dataIndex: 'equipmentName',
      key: 'equipmentName',
    },
    {
      title: '故障描述',
      dataIndex: 'faultDescription',
      key: 'faultDescription',
    },
    {
      title: '报修人员',
      dataIndex: 'repairUserName',
      key: 'repairUserName',
    },
    {
      title: '报修时间',
      dataIndex: 'repairTime',
      key: 'repairTimeList',
      valueType: 'dateTimeRange',
    },
    {
      title: '报修单号',
      dataIndex: 'repairNo',
      key: 'repairNo',
    },
    {
      title: '工单状态',
      dataIndex: 'statusName',
      key: 'statusName',
    },
  ],
};

const columns = `function getcolumns(setdrawer) {
  return ${JSON.stringify(datas, null, 1)};

}
export default getcolumns;`;

const setcolumns = (config, columnes, pathconfig) => {
  if (config.tab) {
    return `function getcolumns(setdrawer) {
      return ${JSON.stringify(config?.tablist, null, 1)};
    }
    export default getcolumns;
    `;
  }
  return `function getcolumns(setdrawer) {
      return ${JSON.stringify(
        {
          columns: columnes,
          pathconfig,
        },
        null,
        1
      )};
    }
    export default getcolumns;
    `;
};

const settablecrud = (title, Component, config, pathconfig) => {
  if (config.tab) {
    return `import * as React from 'react';
    import { useState, useMemo, useRef } from 'react';
    import DrawerPro from '@/components/DrawerPro';
    import AutoTable from '@/components/AutoTable';
    import PremButton from '@/components/PremButton';
    import getcolumns from './columns';
    import { useRequest } from 'ahooks';
    import { doFetch } from '@/utils/doFetch';

    function ${Component}(props) {
      const actionRef = useRef(),
        formRef = useRef();
      const [drawer, setdrawer] = useState({
          open: false,
        }),
        [activeTabKey, setactiveTabKey] = useState(${config?.tablist[0]?.key});

      const { run, loading } = useRequest(doFetch, {
        manual: true,
        onSuccess: (res, params) => {
          if (res?.code == '0000') {
            actionRef?.current?.reload();
            setdrawer((s) => ({
              ...s,
              open: false,
            }));
          }
        },
      });

      const detail = (text, row, _, action) => {
        return (
          <PremButton
            btn={{
              size: 'small',
              type: 'link',
              onClick: () => {
                setdrawer((s) => ({
                  ...s,
                  open: true,
                  item: row,
                  title: '详情',
                  val: 'detail',
                  title: '详细信息',
                }));
              },
            }}
          >
            详情
          </PremButton>
        );
      };

      const edit = (text, row, _, action) => {
        return (
          <PremButton
            btn={{
              size: 'small',
              onClick: () => {
                setdrawer((s) => ({
                  ...s,
                  open: true,
                  item: row,
                  title: '编辑',
                  val: 'edit',
                }));
              },
            }}
          >
            编辑
          </PremButton>
        );
      };

      const remove = (text, row, _, action) => {
        return (
          <PremButton
            pop={{
              title: '是否删除?',
              okText: '确认',
              cancelText: '取消',
              onConfirm: () => {
                run({ url: pathconfig?.delete || '/delete', params: { id: row?.id } });
              },
            }}
            btn={{
              size: 'small',
              type: 'danger',
            }}
          >
            删除
          </PremButton>
        );
      };

      const columns = useMemo(() => {
        let defcolumn = getcolumns(setdrawer).filter((it) => it.key == activeTabKey)[0]?.columns;
        let defpath = getcolumns(setdrawer).filter((it) => it.key == activeTabKey)[0]?.pathconfig ?? {};
        return defcolumn.concat({
          title: '操作',
          valueType: 'option',
          width: 150,
          render: (text, row, _, action) => [
            defpath?.enabledetail && detail(text, row, _, action),
            defpath?.enableedit && edit(text, row, _, action),
            defpath?.enabledelete && remove(text, row, _, action),
          ],
        });
      }, [activeTabKey]);

      const pathconfig = useMemo(() => {
        let defpath = getcolumns(setdrawer).filter((it) => it.key == activeTabKey)[0]?.pathconfig ?? {};
        return defpath;
      }, [activeTabKey]);

      return (
        <div style={{ position: 'relative' }}>
          <AutoTable
            pagetitle="${title}"
            columns={columns}
            path={pathconfig?.list || '/ngic-auth/sysUser/query/page'}
            actionRef={actionRef}
            pageextra={pathconfig?.enableadd ? 'add' : null}
            resizeable={true}
            addconfig={{
              // access: 'sysDepartment_save',
              btn: {
                disabled: false,
                onClick: () => {
                  setdrawer((s) => ({
                    ...s,
                    open: true,
                    item: null,
                    title: '新增',
                    val: 'add',
                  }));
                },
              },
            }}
            tabList={getcolumns()}
            activeTabKey={activeTabKey}
            onTabChange={(key) => {
              setactiveTabKey(key);
            }}
          />

          <DrawerPro
            fields={columns}
            detailpath={pathconfig?.detail || null}
            detailData={drawer?.item}
            defaultFormValue={drawer?.item}
            params={{ id: drawer?.item?.id }}
            formRef={formRef}
            placement="right"
            onClose={() => {
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
            }}
            {...drawer}
            onFinish={(vals) => {
              if (drawer?.val == 'add') {
                run({ url: pathconfig?.add || '/add', params: { ...vals } });
              } else if (drawer?.val == 'edit') {
                run({ url: pathconfig?.edit || '/edit', params: { ...vals, id: drawer?.item?.id } });
              }
            }}
          />
        </div>
      );
    }

    export default ${Component};
    `;
  }
  return `import * as React from 'react';
  import { useState, useMemo, useRef } from 'react';
  import DrawerPro from '@/components/DrawerPro';
  import AutoTable from '@/components/AutoTable';
  import PremButton from '@/components/PremButton';
  import getcolumns from './columns';
  import { useRequest } from 'ahooks';
  import { doFetch } from '@/utils/doFetch';

  function ${Component}(props) {
    const actionRef = useRef(),
      formRef = useRef();
    const [drawer, setdrawer] = useState({
      open: false,
    });
    const pathconfig = useMemo(() => {
      let pathconf = getcolumns(setdrawer)?.pathconfig ?? {};
      return pathconf;
    }, []);
    const { run, loading } = useRequest(doFetch, {
      manual: true,
      onSuccess: (res, params) => {
        if (res?.code == '0000') {
          actionRef?.current?.reload();
          setdrawer((s) => ({
            ...s,
            open: false,
          }));
        }
      },
    });

    const detail = (text, row, _, action) => {
      return (
        <PremButton
          btn={{
            size: 'small',
            type: 'link',
            onClick: () => {
              setdrawer((s) => ({
                ...s,
                open: true,
                item: row,
                title: '详情',
                val: 'detail',
                title: '详细信息',
              }));
            },
          }}
        >
          详情
        </PremButton>
      );
    };

    const edit = (text, row, _, action) => {
      return (
        <PremButton
          btn={{
            size: 'small',
            onClick: () => {
              setdrawer((s) => ({
                ...s,
                open: true,
                item: row,
                title: '编辑',
                val: 'edit',
              }));
            },
          }}
        >
          编辑
        </PremButton>
      );
    };

    const remove = (text, row, _, action) => {
      return (
        <PremButton
          pop={{
            title: '是否删除?',
            okText: '确认',
            cancelText: '取消',
            onConfirm: () => {
              run({ url: pathconfig?.delete || '/delete', params: { id: row?.id } });
            },
          }}
          btn={{
            size: 'small',
            type: 'danger',
          }}
        >
          删除
        </PremButton>
      );
    };

    const columns = useMemo(() => {
      let defcolumn = getcolumns(setdrawer)?.columns;
      return defcolumn.concat({
        title: '操作',
        valueType: 'option',
        width: 150,
        render: (text, row, _, action) => [
          pathconfig?.enabledetail && detail(text, row, _, action),
          pathconfig?.enableedit && edit(text, row, _, action),
          pathconfig?.enabledelete && remove(text, row, _, action),
        ],
      });
    }, []);

    return (
      <div style={{ position: 'relative' }}>
        <AutoTable
          pagetitle="${title}"
          columns={columns}
          actionRef={actionRef}
          path={pathconfig?.list || '/ngic-auth/sysUser/query/page'}
          pageextra={pathconfig?.enableadd ? 'add' : null}
          resizeable={true}
          addconfig={{
            // access: 'sysDepartment_save',
            btn: {
              disabled: false,
              onClick: () => {
                setdrawer((s) => ({
                  ...s,
                  open: true,
                  item: null,
                  title: '新增',
                  val: 'add',
                }));
              },
            },
          }}
        />

        <DrawerPro
          fields={columns}
          params={{ id: drawer?.item?.id }}
          formRef={formRef}
          placement="right"
          detailpath={pathconfig?.detail || null}
          detailData={drawer?.item}
          defaultFormValue={drawer?.item}
          onClose={() => {
            setdrawer((s) => ({
              ...s,
              open: false,
            }));
          }}
          {...drawer}
          onFinish={(vals) => {
            if (drawer?.val == 'add') {
              run({ url: pathconfig?.add || '/add', params: { ...vals } });
            } else if (drawer?.val == 'edit') {
              run({ url: pathconfig?.edit || '/edit', params: { ...vals, id: drawer?.item?.id } });
            }
          }}
        />
      </div>
    );
  }

  export default ${Component};`;
};

export { tablecrud, columns, setcolumns, settablecrud };
